<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">教学体系管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>教学体系管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="study-sys-search-value" class="layui-input search-input" type="text" autocomplete="off" placeholder="输入教学体系名称"/>&emsp;
            <button id="study-sys-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="study-sys-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
<!--            <button id="study-sys-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>-->
        </div>

        <table class="layui-table" id="study-system-table" lay-filter="study-system-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="study-sys-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
<!--    <a class="layui-btn layui-btn-xs" lay-event="look">查看详情</a>-->
</script>

<!-- 表格状态列 -->
<script type="text/html" id="study-sys-tpl-state">
    <input type="checkbox" lay-filter="study-sys-tpl-state" value="{{d.tcstid}}" lay-skin="switch" lay-text="使用|禁用"
           {{d.status==1?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var admin = layui.admin;


        // 添加按钮绑定权限
        /*if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-add').hide();
        }*/

        // 导出按钮绑定权限
        /*if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-export').hide();
        }*/



        // 渲染表格
        table.render({
            elem: '#study-system-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-study/teachingSys/getList',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'tcstid', sort: true, title: '体系编号',width: 110},
                {field: 'ttitle', sort: true, title: '体系名称'},
                {field: 'tsdesc', title: '描述'},
                {field: 'cretim', sort: true, title: '创建时间'},
                {field: 'status', sort: true, templet: '#study-sys-tpl-state', title: '状态',width: 150},
                {align: 'center', toolbar: '#study-sys-table-bar', title: '操作',width: 150}
            ]]
        });
        // 搜索按钮点击事件
        $('#study-sys-btn-search').click(function () {
            var value = $('#study-sys-search-value').val();
            table.reload('study-system-table', {where: {ttitle: value}});
        });

        // 添加按钮点击事件
        $('#study-sys-btn-add').click(function () {
            showEditModel();
        });

        table.on('tool(study-system-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('t_study_sys', data);
            var title = data ? '修改体系' : '添加体系';
            admin.popupCenter({
                title: title,
                path: 'pages/study/studySys_form.html',
                finish: function () {
                    table.reload('study-system-table', {});
                }
            });
        };

        // 修改状态
        form.on('switch(study-sys-tpl-state)', function (obj) {
            layer.load(2);
            var data = new FormData();
            data.append("tcstid", obj.elem.value);
            data.append("status", obj.elem.checked ? 1 : 2);
            $.ajax({
                url: config.base_server + 'api-study/teachingSys/delete',
                data: data,
                type: "post",
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (data) {
                    layer.closeAll('loading');
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                },
                beforeSend: function (xhr) {
                    var token = config.getToken();
                    if (token) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                    }
                }
            });
        })
        // 工具条点击事件
       /*






        // 导出
        $('#user-btn-export').click(function () {
            var url = config.base_server + 'api-user/users/exportUser?access_token='+config.getToken().access_token;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function() {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });*/


    });
</script>